
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-incentiveModuleConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#incentiveModuleConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $quotaReviewTypeDiv = $view.find('#quotaReviewTypeDiv'),
        $rewardReviewTypeDiv = $view.find('#rewardReviewTypeDiv'),
        $rewardIssuedTypeDiv = $view.find('#rewardIssuedTypeDiv'),
        tableIns;

    form.verify(validate);

    initFormValue();

    function initFormValue() {
        if (incentiveModuleConfig) {
            let quotaReviewSwitch = incentiveModuleConfig.quotaReviewSwitch;
            let rewardReviewSwitch = incentiveModuleConfig.rewardReviewSwitch;
            let rewardIssuedSwitch = incentiveModuleConfig.rewardIssuedSwitch;
            form.val("incentiveModuleConfig-form", {
                "moduleConfigId": incentiveModuleConfig.moduleConfigId,
                "tenantId": incentiveModuleConfig.tenantId,
                "quotaReviewSwitch": quotaReviewSwitch == 1 ? 'on' : '',
                "quotaReviewType": incentiveModuleConfig.quotaReviewType,
                "rewardReviewSwitch": rewardReviewSwitch == 1 ? 'on' : '',
                "rewardReviewType": incentiveModuleConfig.rewardReviewType,
                "rewardIssuedSwitch": rewardIssuedSwitch == 1 ? 'on' : '',
                "rewardIssuedType": incentiveModuleConfig.rewardIssuedType,
            });
            if (quotaReviewSwitch == 1) {
                $quotaReviewTypeDiv.show();
            }else {
                $quotaReviewTypeDiv.hide();
            }
            if (rewardReviewSwitch == 1) {
                $rewardReviewTypeDiv.show();
            }else {
                $rewardReviewTypeDiv.hide();
            }
            if (rewardIssuedSwitch == 1) {
                $rewardIssuedTypeDiv.show();
            }else {
                $rewardIssuedTypeDiv.hide();
            }

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'incentiveModuleConfig/search/' + tenantId, null, function (data) {
            var newIncentiveModuleConfig = "";
            if(data.code==200&&data.data.incentiveModuleConfig){
                newIncentiveModuleConfig = data.data.incentiveModuleConfig;
            }
            let quotaReviewSwitch = newIncentiveModuleConfig.quotaReviewSwitch;
            let rewardReviewSwitch = newIncentiveModuleConfig.rewardReviewSwitch;
            let rewardIssuedSwitch = newIncentiveModuleConfig.rewardIssuedSwitch;
            form.val("incentiveModuleConfig-form", {
                "moduleConfigId": newIncentiveModuleConfig.moduleConfigId,
                "tenantId": newIncentiveModuleConfig.tenantId,
                "quotaReviewSwitch": quotaReviewSwitch == 1 ? 'on' : '',
                "quotaReviewType": newIncentiveModuleConfig.quotaReviewType ? newIncentiveModuleConfig.quotaReviewType : '1',
                "rewardReviewSwitch": rewardReviewSwitch == 1 ? 'on' : '',
                "rewardReviewType": newIncentiveModuleConfig.rewardReviewType ? newIncentiveModuleConfig.rewardReviewType : '1',
                "rewardIssuedSwitch": rewardIssuedSwitch == 1 ? 'on' : '',
                "rewardIssuedType": newIncentiveModuleConfig.rewardIssuedType ? newIncentiveModuleConfig.rewardIssuedType : '1',
            });
            if (quotaReviewSwitch == 1) {
                $quotaReviewTypeDiv.show();
            }else {
                $quotaReviewTypeDiv.hide();
            }
            if (rewardReviewSwitch == 1) {
                $rewardReviewTypeDiv.show();
            }else {
                $rewardReviewTypeDiv.hide();
            }
            if (rewardIssuedSwitch == 1) {
                $rewardIssuedTypeDiv.show();
            }else {
                $rewardIssuedTypeDiv.hide();
            }

        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    // 监听额度审核开关
    form.on('switch(quotaReviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $quotaReviewTypeDiv.show();
        }else {
            $quotaReviewTypeDiv.hide();
        }
    })
    // 监听奖励审核开关
    form.on('switch(rewardReviewSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $rewardReviewTypeDiv.show();
        }else {
            $rewardReviewTypeDiv.hide();
        }
    })
    // 监听奖励签发开关
    form.on('switch(rewardIssuedSwitch)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $rewardIssuedTypeDiv.show();
        }else {
            $rewardIssuedTypeDiv.hide();
        }
    })

    form.on('submit(incentiveModuleConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        data.field.quotaReviewSwitch = data.field.quotaReviewSwitch == 'on' ? 1 : 0
        data.field.rewardReviewSwitch = data.field.rewardReviewSwitch == 'on' ? 1 : 0
        data.field.rewardIssuedSwitch = data.field.rewardIssuedSwitch == 'on' ? 1 : 0
        febs.modal.confirm('保存提示', '是否保存配置信息？', function () {
            if (data.field.moduleConfigId && data.field.moduleConfigId.length > 0) {
                febs.postArray(ctx + 'incentiveModuleConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'incentiveModuleConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });


    form.render();


});